package wh.比特位计数;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/10 10:41
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int num = 5;
        System.out.println(countBits(num).toString());
    }

    public static int[] countBits(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            String s = Integer.toString(i, 2);
            char[] chars = s.toCharArray();
            int count = 0;
            for (int j = 0; j < chars.length; j++) {
                if (chars[j] == '1'){
                    count++;
                }
            }
            list.add(count);
        }
        int[] array = list.stream().mapToInt(Integer::valueOf).toArray();
        return array;
    }

}
